package com.llh.pojo.common;

import com.alibaba.fastjson.JSON;
import com.llh.contant.ApplicationContant;
import lombok.Data;
import org.apache.commons.codec.Charsets;
import org.apache.http.Header;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;

/**
 * 构建通用的httpClient和httpPost对象
 */
//@Log4j2
@Data
public class CommonReqTool {
    private static Logger logger= LogManager.getLogger(CommonRespTool.class);
    public CloseableHttpClient httpClient;
    public HttpPost httpPost;

    /*    api_name，接口名称；
        token，用于识别唯一用户的标识；
        params，接口参数，如daily接口中start_date和end_date；
        fields，字段列表，用于接口获取指定的字段，以逗号分隔，如"open,high,low,close"；
        输出参数
        code: 接口返回码，2002表示权限问题。
        msg: 错误信息；
        data: 具体数据，成功的请求包含fields和items字段，fields与items数据一一对齐；*/
    //初始化httpClient对象
    public boolean init(CommonReqEntity reqEntity) {
        try {
            // 获得Http客户端(可以理解为:你得先有一个浏览器;注意:实际上HttpClient与浏览器是不一样的)
            httpClient = HttpClientBuilder.create().build();
            CloseableHttpClient httpClient = HttpClientBuilder.create().build();
            // 创建Post请求
            httpPost = new HttpPost(ApplicationContant.url);
          /*  // 我这里利用阿里的fastjson，将Object转换为json字符串;
            // (需要导入com.alibaba.fastjson.JSON包)
            String jsonString = JSON.toJSONString(reqEntity);
            StringEntity entity = new StringEntity(jsonString, "UTF-8");
            entity.setContentEncoding("text/json");
            // post请求是将参数放在请求体里面传过去的;这里将entity放入post请求体中
            httpPost.setEntity(entity);
//            httpPost.setHeader("Content-Type", "application/json;charset=utf8");
//            httpPost.setHeader("charset", "utf-8");
            setHeader(httpPost);
            Header[] allHeaders = httpPost.getAllHeaders();
            logger.info("请求头:"+httpPost.getAllHeaders());*/
            String jsonString = JSON.toJSONString(reqEntity);
            httpPost.addHeader("Content-Type", "application/json;charset=UTF-8");
            httpPost.setHeader("Accept", "application/json");
            httpPost.setEntity(new StringEntity(jsonString, Charsets.UTF_8));
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }
    //发送post请求
    public CloseableHttpResponse sendPost() {
        try {
            // 由客户端执行(发送)Post请求
            CloseableHttpResponse response = httpClient.execute(httpPost);

            return response;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }

    }
    //设置请求头信息的的私有方法
    private void setHeader(HttpPost httpPost){
//        Accept: application/json;charset=UTF-8
        httpPost.setHeader("Accept","application/json;charset=UTF-8");
//        Accept-Encoding: gzip, deflate, br
        httpPost.setHeader(" Accept-Encoding","gzip, deflate, br");
//        Connection: keep-alive
        httpPost.setHeader("Connection","keep-alive");
//        Content-Length: 304
//        httpPost.setHeader("Content-Length","304");
//        Content-Type: application/json;charset=UTF-8
        httpPost.setHeader("Content-Type","application/json");
//        httpPost.setHeader("Content-Type","application/json;charset=UTF-8");

//        User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/95.0.4638.54 Safari/537.36
        httpPost.setHeader("User-Agent","Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/95.0.4638.54 Safari/537.36");
    }

    /**
     * 直接通过请求体json格式字符串创建请求的方式
     * @param reqEntityString
     * @return
     */
    public boolean init(String reqEntityString){
        httpPost.addHeader("Content-Type", "application/json;charset=UTF-8");
        httpPost.setHeader("Accept", "application/json");
        httpPost.setEntity(new StringEntity(reqEntityString, Charsets.UTF_8));
        return true;
    }

}
